﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PanhuaWin.工厂.车间
{
    public partial class Form产线产废确认 : UIPage
    {
        public int 开始行 { get; private set; }
        public int 结束行 { get; private set; }
        public Form产线产废确认()
        {
            InitializeComponent();
        }

        private void uiButton刷新_Click(object sender, EventArgs e)
        {
            List辅料产废();
        }

        private void List辅料产废()
        {
            string sql;
            sql = @" SELECT  row_number() over(order by CreateDate,WasteGuid) 序号,  WasteGuid, WasteType,  WasteDate,WorkShopCode,  InventoryCode, InventoryName, Size, Batch, QuantityMain,Unit, 
                    CreateID, Creator, CreateDate,  WorkLineName,TeamName,Status  " +
                " FROM      Bus_Waste  where 1=1 and WasteDate>='" + this.uiDatePicker开始日期.Value + "' and WasteDate<'" + this.uiDatePicker结束日期.Value.AddDays(1) + "'";
            if (this.uiComboBox产线.SelectedText != "全部")
            {
                sql += " and WorkLineName='" + this.uiComboBox产线.SelectedText + "'";
            }
            if (this.uiComboBoxWorkTeam.SelectedText != "全部")
            {
                sql += " and TeamName='" + this.uiComboBoxWorkTeam.SelectedText + "'";
            }
            if (this.uiTextBox存货编号.Text != "")
            {
                sql += " and InventoryCode='" + this.uiTextBox存货编号.Text.Replace("--", "") + "'";
            }
            if (this.uiRadioButton酸洗.Checked)
            {
                sql += " and WasteType='酸连轧'";
            }
         
            if (this.uiRadioButton镀锌.Checked)
            {
                sql += " and WasteType='镀锌'";
            }
            if (this.uiRadioButton彩涂.Checked)
            {
                sql += " and WasteType='彩涂'";
            }
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                this.uiDataGridView临时.AutoGenerateColumns = false;
                this.uiDataGridView临时.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                this.uiDataGridView临时.DataSource = tb;
                this.uiDataGridView临时.Refresh();
            }
            else
            {
                this.uiDataGridView临时.AutoGenerateColumns = false;
                this.uiDataGridView临时.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
                this.uiDataGridView临时.DataSource = null;
                this.uiDataGridView临时.Refresh();
            }
        }

        private void uiButton导出_Click(object sender, EventArgs e)
        {

        }

        private void uiButton存货查询_Click(object sender, EventArgs e)
        {

        }

        private void uiComboBox产线_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql;
            string WorkLineName = this.uiComboBox产线.SelectedText;
            sql = " select '全部' as WorkTeamCode ";
            sql += " union all ";

            sql += "  select  distinct  WorkTeamCode  from  [Dic_WorkLine]   join Dic_TeamLine on ";
            sql += "[Dic_WorkLine].WorkLineCode = dbo.Dic_TeamLine.WorkLineCode  where 1=1  ";
            if (WorkLineName != "全部")
            {
                sql += " and  Dic_WorkLine.WorkLineName ='" + WorkLineName + "'";
            }
            // sql += " order by WorkTeamCode ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                this.uiComboBoxWorkTeam.DataSource = tb;
                this.uiComboBoxWorkTeam.DisplayMember = "WorkTeamCode";
                this.uiComboBoxWorkTeam.ValueMember = "WorkTeamCode";
                this.uiComboBoxWorkTeam.Refresh();
            }
            else
            {
                this.uiComboBoxWorkTeam.DataSource = null;
            }
        }


        private void uiDataGridView临时_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (this.uiDataGridView临时.Rows.Count > 0)
            {
                if (this.uiDataGridView临时.Columns[e.ColumnIndex].Name == "C确认")
                {
                    string sql;
                    string Cbguid = this.uiDataGridView临时.Rows[e.RowIndex].Cells["Cbguid"].Value.ToString();
                    string 操作人 = this.uiDataGridView临时.Rows[e.RowIndex].Cells["C操作人"].Value.ToString();
                    //检查是不是已经确认
                    if (!是否已经确认(Cbguid))
                    {
                        单行确认(Cbguid);

                    }
                    else
                    {
                        MessageBox.Show("已经被确认不能再次确认了", "", MessageBoxButtons.OK);
                        return;
                    }
                    List辅料产废();
                }
                else if (this.uiDataGridView临时.Columns[e.ColumnIndex].Name == "C取消确认")
                {
                    string sql;
                    string Cbguid = this.uiDataGridView临时.Rows[e.RowIndex].Cells["Cbguid"].Value.ToString();
                    string 操作人 = this.uiDataGridView临时.Rows[e.RowIndex].Cells["C操作人"].Value.ToString();
                    //检查是不是已经确认
                    if (是否已经确认(Cbguid))
                    {
                        单行取消确认(Cbguid);

                    }
                    else
                    {
                        MessageBox.Show("尚未确认不能取消了", "", MessageBoxButtons.OK);
                        return;
                    }
                    List辅料产废();

                }

            }
        }

        private void 单行取消确认(string cbguid)
        {
            string sql;
            sql = "update Bus_Waste set status='未确定' where   WasteGuid='" + cbguid + "'  ";
            pub.ExecuteSql(pub.ConnectionString, sql);
        }

        private void 单行确认(string cbguid)
        {
            string sql;
            sql = "update Bus_Waste set status='确认' where   WasteGuid='" + cbguid + "'  ";
            pub.ExecuteSql(pub.ConnectionString, sql);
        }

        private bool 是否已经确认(string cbguid)
        {
            string sql;
            sql = "select Status from Bus_Waste where WasteGuid='" + cbguid + "' ";
            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            if (tb.Rows.Count > 0)
            {
                if (tb.Rows[0]["Status"].ToString() == "未确定")
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }

        private void uiDataGridView临时_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            开始行 = e.RowIndex;
        }

        private void Form产线产废确认_Load(object sender, EventArgs e)
        {
            this.uiDatePicker开始日期.Value = DateTime.Today;
            this.uiDatePicker结束日期.Value = DateTime.Today;
            ListWorkLine("", this.uiComboBox产线);
            开始行 = -1;
            结束行 = -1;
        }

        private void ListWorkLine(string WorkType, UIComboBox uiComboBox产线)
        {
            string sql;
            sql = @"SELECT '' WorkLineCode,'全部' WorkLineName
                    union all
                    SELECT   WorkLineCode, WorkLineName
                    FROM      Dic_WorkLine where 1=1 ";
            if (WorkType != "")
            {
                sql += " and WorkType='" + WorkType + "'";

            }
            sql += "  order by WorkLineCode ";

            DataTable tb = pub.GetDataTable(pub.ConnectionString, sql);
            uiComboBox产线.DataSource = tb;
            uiComboBox产线.DisplayMember = "WorkLineName";
            uiComboBox产线.ValueMember = "WorkLineName";
            uiComboBox产线.Refresh();


        }

        private void uiDataGridView临时_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            string guid = Guid.NewGuid().ToString();
            decimal 数量 = 0;
            int 行数 = 0;
            结束行 = e.RowIndex;

            if (开始行 == 0 && 结束行 == 0)
            {
                this.uiTextBox行数.Text = "1";
                this.uiTextBox数量.Text = this.uiDataGridView临时.Rows[开始行].Cells["C主数量"].Value.ToString();
            }
            if (开始行 >= 0 && 结束行 > 0)
            {
                if (开始行 > 结束行)
                {
                    int k = 开始行;
                    开始行 = 结束行;
                    结束行 = k;
                }
                for (int i = 开始行; i <= 结束行; i++)
                {
                    数量 += pub.Todecimal(this.uiDataGridView临时.Rows[i].Cells["C主数量"].Value.ToString());
                    行数++;
                }
                this.uiTextBox行数.Text = 行数.ToString();
                this.uiTextBox数量.Text = 数量.ToString();
            }
        }
    }
}
